package cn.tansanqinger.controller.login_register;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tansanqinger.pojo.User;
import cn.tansanqinger.service.user.impl.UserMessageServiceImpl;
import cn.tansanqinger.service.verification.IVerificationMessageService;
import cn.tansanqinger.service.verification.IVerificationService;
import cn.tansanqinger.util.Encoder_Dispose;

@Controller
public class LoginRegisterMessageController {
	
	@Autowired
	private IVerificationMessageService messageService;
	@Autowired
	private IVerificationService iVerificationService;
	@Autowired
	private UserMessageServiceImpl uImpl;
	
	/**
	 * 账号登陆的时候进行账号和密码的验证
	 * @param request 用于传参
	 * @return 返回执行结果
	 */
	@RequestMapping("/loginVerify")
	public String loginVerify(HttpServletRequest request, HttpServletResponse response) {
		String account = request.getParameter("account");
		String passwd = request.getParameter("passwd");
		HttpSession session = request.getSession();
		User user;
		if(account.length()==11) {
			user= uImpl.selectOnePhone(account);
			if(Encoder_Dispose.encoder(passwd).equals(user.getPasswd())) {
				session.setAttribute("user", user);
				Cookie cookie = new Cookie("userEmail", user.getEmail());
				cookie.setMaxAge(30*1000);
				response.addCookie(cookie);
				return "redirect:/index";
			}
				session.setAttribute("error", "请检查你的账号和密码是否匹配");
				return "redirect:/login";
		} else {
			user = uImpl.selectOneEmail(account);
			if(Encoder_Dispose.encoder(passwd).equals(user.getPasswd())) {
				session.setAttribute("user", user);
				Cookie cookie = new Cookie("user", user.getEmail());
				cookie.setMaxAge(30*1000);
				response.addCookie(cookie);
				return "redirect:/index";
			}
			session.setAttribute("error", "请检查你的账号和密码是否匹配");
			return "redirect:/login";
		}
	}
	
	/**
	 * 该方法用于注册账号的时候使用
	 * @param request 传参
	 * @param model 
	 * @return 返回页面
	 */
	@RequestMapping("/registersubmtMessage")
	public String registersubmtMessage(HttpServletRequest request, Model model) {
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		String passwd = request.getParameter("passwd1");
		String verificationCodeEmail = request.getParameter("verificationCodeEmail");
		String emailVerification = messageService.readEmailMessageNoSQL(email);
		HttpSession session = request.getSession();//获取session
		if(emailVerification!=null&&verificationCodeEmail!=null) {
			if(verificationCodeEmail.equalsIgnoreCase(emailVerification)) {
				if(uImpl.addUser(email, phone, passwd)) {
					session.setAttribute("message", "账号注册成功！");
					return "redirect:/login";
				}else {
					session.setAttribute("error", "注册失败，请认真填写数据！");
					return "redirect:/login/register";
				}
			}else {
				session.setAttribute("error", "邮箱验证码错误！");
				return "redirect:/login/register";
			}
		} else {
			session.setAttribute("error", "你输入的验证码已经过时了");
			return "redirect:/login/register";
		}
	}
	
	/**
	 * 用于修改密码时调用
	 * @param request 传参
	 * @return 返回执行结果
	 */
	@RequestMapping("/modificationPasswd")
	public String modificationPasswd(HttpServletRequest request) {
		String email = request.getParameter("email");//获取邮箱账号
		String passwd = request.getParameter("passwd");//获取新密码
		String verificationCodeEmail = request.getParameter("verificationCodeEmail");//获取邮箱验证码
		String emailVerification = messageService.readEmailMessageNoSQL(email);//获取NoSQL中的验证码
		HttpSession session = request.getSession();//获取session
		if(emailVerification!=null) {
			if(verificationCodeEmail.equalsIgnoreCase(emailVerification)) {//比较验证码是否一致
				String passwdEncoder = Encoder_Dispose.encoder(passwd);
				if(passwdEncoder.equals(uImpl.selectOneEmail(email).getPasswd())) {//检查是否密码重复了
					session.setAttribute("error", "新密码不能和原始密码一致");
					return "redirect:/login/passwd";
				}else {
					if(uImpl.updatePasswd(email, passwdEncoder)) {
						session.setAttribute("message", "密码修改成功");
						return "redirect:/login";
					}else {
						session.setAttribute("error", "密码修改失败");
						return "redirect:/login/passwd";
					}
				}
			}else {
				session.setAttribute("error", "邮箱验证码错误！");
				return "redirect:/login/passwd";
			}
		}else {
			session.setAttribute("error", "你输入的验证码已经过时了");
			return "redirect:/login/passwd";
		}
		
	}
	
	/**
	 * 判断该邮箱是否被注册过
	 * @param email 邮箱账号
	 * @return 返回结果
	 */
	@RequestMapping("/emailExists/{email}")
	@ResponseBody
	public boolean emailExists(@PathVariable("email") String email){
		return uImpl.isEmail(email);
	}
	
	/**
	 * 判断该电话号码是否被注册过
	 * @param phone 手机号
	 * @return 返回查询结果
	 */
	@RequestMapping("/phoneExists/{phone}")
	@ResponseBody
	public boolean phoneExists(@PathVariable("phone") String phone){
		return uImpl.isPhone(phone);
	}
	
	@RequestMapping("/verificationExists/{verification}/{email}")
	@ResponseBody//1验证码正确，2验证码错误，3邮件发送失败
	public boolean verificationExists(@PathVariable("verification") String verification, 
			@PathVariable("email") String email, HttpServletRequest request){
		String message = messageService.readEmailMessageNoSQL(request.getSession().getId());
		if(verification.equalsIgnoreCase(message)) {
			iVerificationService.emailVerification(email);
			return true;
		}else{
			return false;
		}
	}
	
	@RequestMapping("/loginVerify/{code}")
	@ResponseBody
	public boolean verificationExists(@PathVariable("code") String code, HttpServletRequest request){
		return code.equalsIgnoreCase(messageService.readEmailMessageNoSQL(request.getSession().getId()));
	}
}
